<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.filesystem.folder');

class TableProduct extends JTable {
    var $id = null;
    var $serial_code = null;
    var $pond_code = null;
    var $class_code = null;
    var $customer_code = null;
    var $feed_date = null;
    var $sale_date = null;
    var $expired_date = null;
    var $status = null;   

    function __construct( &$_db ) {
        parent::__construct( '#__product', 'id', $_db );
    }
    
	function deleteAll($aid, $where = '') {
        JArrayHelper::toInteger($aid);
        
        foreach ($aid AS $id) {
			$this->load($id);
        }
     
        if(!is_array($aid) ) {
            $aid = array(intval($aid));
        }
            
        $query = 'DELETE FROM ' . $this->_db->nameQuote($this->_tbl) .
            	' WHERE ' . $this->_tbl_key . ' IN (' . implode(',', $aid) . ')' .
        		((!empty($where)) ? $where : '');

        $this->_db->setQuery($query);
        if ($this->_db->query()) {
            return true;
        } else {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
    }
    
    function saveRecord($data) {
    	$config = JModel::getInstance('Config', 'ManagerModel');
    	$model_serial = JModel::getInstance('Serial', 'PimModel');
    	
        $obj_config = $config->getConfigByKey("duration_expired");
        
        $num_expired_value = intval($obj_config->pim_value);
		$sale_date = date("Y-m-d H:i:s", strtotime($data['sale_date']));
        
		$data['feed_date'] = date("Y-m-d H:i:s", strtotime($data['feed_date']));
        $data['sale_date'] = $sale_date;
        $data['expired_date'] = ManagerHelper::getExpiredDate($sale_date, $num_expired_value);

		if ($this->save($data)) {
			$model_serial->updateStatus($data['serial_code'], ManagerHelper::SERIAL_STATUS_USED, ManagerHelper::SERIAL_TYPE_OPEN);
		}
    }
    
}